# 导入pymysql模块
import pymysql

class Mysql():
    def __init__(self):
        # 建立数据库连接
        self.conn = pymysql.connect(
            host='106.15.50.65',		# 主机名（或IP地址）
            port=3306,				# 端口号，默认为3306
            user='xkk',			# 用户名
            database="questionnaire",
            password='xkkxkkxkk',	# 密码
        )
    def selectDB(self,sql,type='many'):
        cursor = self.conn.cursor(pymysql.cursors.DictCursor)
        cursor.execute(sql)
        result = cursor.fetchall()
        cursor.close()
        self.conn.close()
        if result is None or len(result)==0:
            if type=='many':
                return []
            else:
                return None
        if type=='many':
            return result
        else:
            return result[0]
    
    def insertDB(self,sql):
        cursor = self.conn.cursor(pymysql.cursors.DictCursor)
        cursor.execute(sql)
        rowCount = cursor.rowcount
        self.conn.commit()
        cursor.close()
        self.conn.close()
        return rowCount